package com.vodone.v1pin.core.utils
{
	import com.vodone.v1pin.core.ExternalInterfaceManager;
	import com.vodone.v1pin.core.model.MainModelLocator;
	import com.vodone.v1pin.detail.vo.ProductDetailVO;
	import com.vodone.v1pin.productlist.vo.ProductItemVO;
	import com.vodone.v1pin.user.model.BagModelLocator;
	import com.vodone.v1pin.user.vo.MyGoodsVO;
	import com.vodone.v1pin.userInfo.delegate.LoveDelegate;
	import com.vodone.v1pin.userInfo.event.CloseWindowEvent;
	
	import flash.net.SharedObject;
	
	import mx.collections.ArrayCollection;

	public class GoodsUtil
	{
		/**
		 * 添加到我的购物袋
		 * @param list
		 * 每一个元素是一个object  属性(id:商品id  count：商品数量)
		 */		
		public static function saveGoods( id:String , count:int = 1 ):void
		{
			var v1pinGoods:Object = SharedObject.getLocal("v1pinGoods");
			var myGoods:Object;
			if(v1pinGoods.data.myGoods){
				myGoods = v1pinGoods.data.myGoods;
			}else{
				v1pinGoods.data.myGoods = new Object();
				myGoods = v1pinGoods.data.myGoods;
			}
			
			if(myGoods[id]){
				myGoods[id].count += count;
			}else{
				myGoods[id] = new Object();
				myGoods[id].count = count;
			}
			BagModelLocator.getInstance().reFreshMyGoods();
		}
		
		/**
		 * 存储用户名密码
		 * @param list
		 * 每一个元素是一个object  属性(id:商品id  count：商品数量)
		 */		
		public static function saveUserName( um:String ,isRem:Boolean ):void
		{
			var userInfo:Object = SharedObject.getLocal("userInfo");
			var userInfoObj:Object;
			if(userInfo.data.userInfoObj){
				userInfoObj = userInfo.data.userInfoObj;
			}else{
				userInfo.data.userInfoObj = new Object();
				userInfoObj = userInfo.data.userInfoObj;
			}
			
			userInfoObj["un"] = um;
			userInfoObj["rem"] = isRem;
		}
		
		/**
		 * 获取用户名
		 */		
		public static function getUserName():Object
		{
			var userInfo:Object = SharedObject.getLocal("userInfo");
			if(userInfo.data.userInfoObj){
				return userInfo.data.userInfoObj;
			}
			return null;
		}
		/**
		 * 删除购物车商品 
		 * @param count
		 * @param deleteAll
		 * 
		 */		
		public static function deleteGoods( id:String , count:int = 1 , deleteAll:Boolean = false ):void
		{
			var v1pinGoods:Object = SharedObject.getLocal("v1pinGoods");
			if(v1pinGoods.data.myGoods){
				var myGoods:Object = v1pinGoods.data.myGoods;
				if( myGoods[id] ){
					if( deleteAll ){
						delete myGoods[id];
						BagModelLocator.getInstance().reFreshMyGoods();
						return;
					}else{
						myGoods[id].count -= count;
					}
					if( myGoods[id].count <=0 ){
						delete myGoods[id];
					}
				}
			}
			BagModelLocator.getInstance().reFreshMyGoods();
		}
		
		/**
		 * 获取购物袋里的商品 
		 */		
		public static function getGoods():Object
		{
			var v1pinGoods:Object = SharedObject.getLocal("v1pinGoods"); 
			if(v1pinGoods.data.myGoods){
				return v1pinGoods.data.myGoods;
			}
			return {};
		}
		
		/**
		 * 获取购物袋里的商品 
		 */		
		public static function getGoodsById( id:String ):Object
		{
			var v1pinGoods:Object = SharedObject.getLocal("v1pinGoods"); 
			if(v1pinGoods.data.myGoods){
				return v1pinGoods.data.myGoods[id];
			}
			return {};
		}
		
		/**
		 * 存储浏览记录 
		 * @return 
		 * 
		 */		
		public static function saveHistoryGoodsById( item:ProductDetailVO ):void
		{
			var v1pinGoods:Object = SharedObject.getLocal("v1pinGoods");
			var myGoods:Object;
			if(v1pinGoods.data.myHistoryGoods){
				myGoods = v1pinGoods.data.myHistoryGoods;
			}else{
				v1pinGoods.data.myHistoryGoods = new Object();
				myGoods = v1pinGoods.data.myHistoryGoods;
			}
			
			myGoods[item.id] = item;
		}
		
		/**
		 * 删除浏览记录 
		 * @return 
		 * 
		 */		
		public static function deleteHistoryGoodsById( id:String ):void
		{
			var v1pinGoods:Object = SharedObject.getLocal("v1pinGoods");
			var myGoods:Object;
			if(v1pinGoods.data.myHistoryGoods){
				myGoods = v1pinGoods.data.myHistoryGoods;
			}else{
				v1pinGoods.data.myHistoryGoods = new Object();
				myGoods = v1pinGoods.data.myHistoryGoods;
			}
			
			if(myGoods[id]){
				delete myGoods[id];
			}
		}
		
		/**
		 * 获取浏览记录 
		 * @return 
		 * 
		 */		
		public static function getHistoryGoods():Array
		{
			var v1pinGoods:Object = SharedObject.getLocal("v1pinGoods");
			var myGoods:Object;
			if(v1pinGoods.data.myHistoryGoods){
				myGoods = v1pinGoods.data.myHistoryGoods;
			}else{
				v1pinGoods.data.myHistoryGoods = new Object();
				myGoods = v1pinGoods.data.myHistoryGoods;
			}
			
			var list:Array = [];
			for(var i:String in myGoods){
				var vo:MyGoodsVO = new MyGoodsVO();
				var item:Object = myGoods[i];
				if(item){
					vo.prodouctID = item.id;
					vo.price = item.price;
					vo.name = item.name;
					vo.v1Price = item.v1pinPrice;
					vo.url = item.mainPic.extend;
					vo.categoryCode = item.number;
					list.push(vo);
				}
			}
			return list;
		}
		/**
		 * 获取购物袋里的商品 的总数
		 */		
		public static function getGoodsNum():int
		{
			var v1pinGoods:Object = SharedObject.getLocal("v1pinGoods"); 
			if(v1pinGoods.data.myGoods){
				var len:int = 0;
				for( var i:String in v1pinGoods.data.myGoods){
					len ++;
				}
				return len;
			}
			return 0;
		}
		
		/**
		 * 收藏商品
		 */		
		public static function collectGoods( id:String ):void
		{
//			var favorGoods:SharedObject = SharedObject.getLocal("v1pinFavorGoods");
//			favorGoods.data.list ||= [];
//			var list:Array = favorGoods.data.list as Array;
//			if( list.indexOf( id ) < 0  )
//			{
//				list.push( id );
//				favorGoods.flush();
//			}
			LoveDelegate.addMyLove( collectResultHandler1 , id , 0 );
		}
		public static function collectResultHandler1( obj:Object  ):void
		{
			if(obj)
			{
				new CloseWindowEvent(CloseWindowEvent.CLOSE_WINDOW_LOVE_EVENT).dispatch();
			}
		}
			
		public static function haveCollect( id:String ):Boolean
		{
			var favorGoods:SharedObject = SharedObject.getLocal("v1pinFavorGoods");
			favorGoods.data.list ||= [];
			var list:Array = favorGoods.data.list as Array;
			return list.indexOf( id ) >= 0 
		}
		
		/**
		 * 
		 * 大图	http://v1admin.suizui.net/Pics/big/
			中图	http://v1admin.suizui.net/Pics/middel/
			小图	http://v1admin.suizui.net/Pics/small/
		 * 
		 */		
		CONFIG::isReal
		public static function getBigPicUrl( path:String ):String
		{
			return MainModelLocator.getInstance().config.imageUrl +"Form/" + path;
		}
		CONFIG::isMock
		public static function getBigPicUrl( path:String ):String
		{
			return "" + path;
		}
		
		CONFIG::isReal
		public static function get200PicUrl( path:String ):String
		{
			return MainModelLocator.getInstance().config.imageUrl +"200/" + path;
		}
		CONFIG::isMock
		public static function get200PicUrl( path:String ):String
		{
			return "" + path;
		}
		
		CONFIG::isReal
		public static function get300PicUrl( path:String ):String
		{
			return MainModelLocator.getInstance().config.imageUrl +"300/" + path;
		}
		CONFIG::isMock
		public static function get300PicUrl( path:String ):String
		{
			return "" + path;
		}
		
		CONFIG::isReal
		public static function getMiddelPicUrl( path:String ):String
		{
			var str:String = MainModelLocator.getInstance().config.imageUrl +"450/" + path;
			return str;
		}
		CONFIG::isMock
		public static function getMiddelPicUrl( path:String ):String
		{
			return "" + path;
		}
		CONFIG::isReal
		public static function get120PicUrl( path:String ):String
		{
			return MainModelLocator.getInstance().config.imageUrl +"120/" + path;
		}
		CONFIG::isMock
		public static function get120PicUrl( path:String ):String
		{
			return "" + path;
		}
		CONFIG::isReal
		public static function getSmallPicUrl( path:String ):String
		{
			return MainModelLocator.getInstance().config.imageUrl +"50/" + path;
		}
		CONFIG::isMock
		public static function getSmallPicUrl( path:String ):String
		{
			return "" + path;
		}
	}
}